import { Component, OnInit } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzModalService } from 'ng-zorro-antd/modal';
import { format, subDays } from 'date-fns';
import { ActivatedRoute } from '@angular/router';
import { profitExamineArrs, profitArrivalArrs } from '../status.pipe';
import { CommonService } from '../../../services-common/common.service';
import { CustomerUsercashFlowService } from '../customer-usercash-flow/customer-usercash-flow.service';

@Component({
  selector: 'app-customer-cash-flow',
  templateUrl: './customer-cash-flow.component.html',
  styleUrls: ['./customer-cash-flow.component.scss']
})
export class CustomerCashFlowComponent implements OnInit {

  routeParams: any = {};
  searchOptions: any = [
    { type: 1, labelText: '综合搜索', valueKey: 'user', value: '', placeholder: 'ID/关联单号/交易流水' },
    { type: 3, labelText: '发生时间', valueKey: ['beginTime', 'endTime'], value: [], isTimeSelect: true },
    {
      type: 2,
      labelText: '收支类型',
      valueKey: 'transactionType',
      value: [],
      multiple: true,
      notTostr: false,
      selectBindKey: { key: 'content', label: 'name' },
      selectOptions: []
    },
    {
      type: 2,
      labelText: '二级分类',
      valueKey: 'appId',
      value: [],
      multiple: true,
      notTostr: false,
      selectBindKey: { key: 'id', label: 'name' },
      selectOptions: []
    },
    {
      type: 2,
      labelText: '收益到账状态',
      valueKey: 'status',
      value: [],
      multiple: true,
      notTostr: false,
      selectBindKey: { key: 'id', label: 'label' },
      selectOptions: [...profitArrivalArrs]
    }
  ];
  searchParams: any = {};

  tableParams: any = {
    loading: false,
    tableData: [],
    page: 1,
    pageSize: 20,
    total: 0
  };

  collectBranchOptions: Array<any> = []; // 现金流水类型options
  exportLoading = false;

  constructor(
    private msg: NzMessageService,
    private modal: NzModalService,
    private activatedRoute: ActivatedRoute,
    private commonService: CommonService,
    private services: CustomerUsercashFlowService
  ) {
    activatedRoute.params.subscribe((res: any) => {
      this.routeParams = res || {};
    })
  }

  ngOnInit(): void {
    this.searchOptions[1].value = this.setDefaultTime(7);
    this.loadReceiptStausData();
    this.loadGamelist();
  }

  // 查询收支类型
  async loadReceiptStausData() {
    this.collectBranchOptions = await this.commonService.getDictList('user_account_history_type');
    this.setSearchOption(this.collectBranchOptions, 'transactionType');
  }

  // 查询应用列表
  async loadGamelist() {
    let params = {
      page: 1,
      pageSize: 9999
    }
    const {code, data, message}: any = await this.services.getGameListData(params);

    if (code === 0) {
      this.setSearchOption(data.records || [], 'appId');
    } else {
      this.msg.error(message);
    }
  }

  async loadTable() {
    let params = {
      ...this.searchParams,
      userId: this.routeParams.id,
      source: 1, // 0 表示现金流水 1 表示用户现金流水
      page: this.tableParams.page,
      pageSize: this.tableParams.pageSize
    }
    
    this.tableParams.loading = true;
    const {code, data, message}: any = await this.services.getUserCashFlowList(params);
    this.tableParams.loading = false;

    if (code === 0) {
      const {records, total} = data || {};

      this.tableParams.tableData = records || [];
      this.tableParams.total = total || 0;
    } else {
      this.msg.error(message);
    }
  }

  queryData(params: any) {
    this.searchParams = params;
    this.tableParams.page = 1;
    this.loadTable();
  }

  resetData(params: any) {
    this.searchOptions[1].value = this.setDefaultTime(7);
    params.beginTime = format(subDays(new Date(), 7), 'yyyy-MM-dd');
    params.endTime = format(new Date(), 'yyyy-MM-dd');

    this.searchParams = params;
    this.tableParams.page = 1;
    this.loadTable();
  }

  // PageIndex Change
  onPageIndexChange(index: number) {
    this.tableParams.page = index;
    this.loadTable();
  }
  // PageSize Change
  onPageSizeChange(sizeNum: number) {
    this.tableParams.page = 1;
    this.tableParams.pageSize = sizeNum;
    this.loadTable();
  }

  setSearchOption(array: Array<any>, val: string) {
    let temIndex = this.searchOptions.findIndex((item: any) => item.valueKey === val);
    this.searchOptions[temIndex].selectOptions = array || [];
  }

  setDefaultTime(dyaNum: number) {
    return [format(subDays(new Date(), dyaNum), 'yyyy-MM-dd'), format(new Date(), 'yyyy-MM-dd')];
  }

  // Filter Text
  filterText(arrs: Array<any>, keyVal: any, idKey: string) {
    let tempObjs: any = {};
    if (arrs.length) {
      tempObjs = arrs.find(item => item[idKey] == keyVal);
    }

    return tempObjs?.name || '-';
  }
}
